"use strict";
const common_vendor = require("../common/vendor.js");
const utils_request = require("../utils/request.js");
const _sfc_main = {
  __name: "LoginPopup",
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  emits: ["update:modelValue", "success"],
  setup(__props, { emit: __emit }) {
    const emit = __emit;
    const userAccount = common_vendor.ref("");
    const userPassword = common_vendor.ref("");
    const agreed = common_vendor.ref(false);
    const canSubmit = common_vendor.computed(() => {
      return userAccount.value.length >= 4 && userPassword.value.length >= 6 && agreed.value;
    });
    const handleClose = () => {
      emit("update:modelValue", false);
    };
    const handleAgree = (e) => {
      agreed.value = e.detail.value.length > 0;
    };
    const handleLogin = async () => {
      if (!canSubmit.value)
        return;
      try {
        const res = await utils_request.request({
          url: "/api/user/login",
          method: "POST",
          data: {
            userAccount: userAccount.value,
            userPassword: userPassword.value
          }
        });
        if (res.code === 0) {
          const userInfo = {
            isLogin: true,
            id: res.data.id,
            avatar: res.data.userAvatar || "/static/avatar/default-avatar.png",
            nickname: res.data.userName,
            desc: res.data.userProfile || "这个人很懒，什么都没写~",
            role: res.data.userRole
          };
          common_vendor.index.setStorageSync("userInfo", userInfo);
          emit("success", userInfo);
          emit("update:modelValue", false);
          common_vendor.index.showToast({
            title: "登录成功",
            icon: "success"
          });
        } else {
          common_vendor.index.showToast({
            title: res.message || "登录失败",
            icon: "none"
          });
        }
      } catch (error) {
        console.error("登录失败:", error);
        common_vendor.index.showToast({
          title: "登录失败，请重试",
          icon: "none"
        });
      }
    };
    const showAgreement = () => {
      common_vendor.index.navigateTo({
        url: "/pages/agreement/user"
      });
    };
    const showPrivacy = () => {
      common_vendor.index.navigateTo({
        url: "/pages/agreement/privacy"
      });
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: __props.modelValue
      }, __props.modelValue ? {
        b: common_vendor.o(handleClose),
        c: common_vendor.o(handleClose),
        d: userAccount.value,
        e: common_vendor.o(($event) => userAccount.value = $event.detail.value),
        f: userPassword.value,
        g: common_vendor.o(($event) => userPassword.value = $event.detail.value),
        h: common_vendor.o(handleLogin),
        i: !canSubmit.value,
        j: agreed.value,
        k: common_vendor.o(handleAgree),
        l: common_vendor.o(showAgreement),
        m: common_vendor.o(showPrivacy)
      } : {});
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-dc57fbbe"]]);
wx.createComponent(Component);
